<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$lst = new ListA;
class ListA
{
	function ListA()
	{
		global $BIZ,$INCOME,$ID,$DB;

		if($BIZ->is_cached("List.tpl",$BIZ->seller['seller_id'],$BIZ->cache_id))
		{
			$BIZ -> output("List.tpl",true,$BIZ->seller['seller_id']);
		}

        define("PAGE_NUM",30);

	    $BIZ -> assign( "code",$INCOME['code'] );
		$BIZ -> assign( "FieldID",$INCOME['FieldID']);

		//read diamond shop by yejun shop_set_top==1 为钻石 2为诚信
		$DB -> db_query( "SELECT s . * , p.pic_name AS pn FROM biz_shop s, biz_pic p WHERE p.pic_id = s.shop_prime_picture_id AND p.pic_id <>0 AND s.shop_check_status ='checked' AND s.shop_ban_status ='off' AND shop_set_top = '1' ORDER BY s.shop_global_recommend DESC LIMIT 0 , 7 " );

		$aDiamond = array();
		
		while( $d = $DB -> db_fetch_row() )
		{
 			$aDiamond[] = array($d['shop_id'],$d['shop_name'],$BIZ->get_thumb($d['pn']));
		}
		$BIZ -> assign("aDiamond",$aDiamond);
		
		switch( $INCOME['code'] )
		{
			case 'g':
				$BIZ -> assign( "list_nav","全部商品");
			    $this -> list_goods("");
				break;
			case 'lg':
				$BIZ -> assign( "list_nav","一元区商品区"); //一元区商品
				$this -> list_goods("low");
			    break;
			case 's':
				$BIZ -> assign( "list_nav","分类商铺");
				$this -> list_shop($INCOME[id]);
			    break;
			case 'gold':
				$BIZ -> assign( "list_nav","黄金商家" );
			    $this -> list_user(3);
				break;
			case 'trust':
				$BIZ -> assign( "list_nav","信誉商家" );
				$this -> list_user(2);
				break;
			case 'search':
				$this -> do_search();
			    break;
			default:
				$BIZ -> assign( "list_nav","全部商品");
			    $INCOME['code']='g';
			    $this -> list_goods("");
			   break;
		}
		
		$BIZ -> output("List.tpl",true,$BIZ->seller['seller_id']);
	}
	
	

	function list_shop($id)
	{
		global $BIZ,$DB,$INCOME;

		//---------------------------------------------------
		//判断搜索条件
		//---------------------------------------------------
			if($id) $condition = " AND shop_property_id = '$id' ";
		//---------------------------------------------------
		//判断排序条件
		//---------------------------------------------------
        switch($INCOME[FieldID])
		{
			case '1':
				$order = " ORDER BY `shop_create_time` DESC ";
			    break;
			case '2':
				$order = " ORDER BY `shop_create_time` ASC ";
				break;
			default:
				$order = "  ORDER BY `shop_create_time` DESC ";
			    break;
		}

		//------------------------------------
		//paging
		//------------------------------------
		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		
		$DB -> db_query( "SELECT count(*) as count FROM biz_shop WHERE shop_check_status ='checked' AND shop_ban_status='off' ".$condition );
        
		$row_count = $DB -> db_fetch_row();

		if ($row_count['count'] < 1)
				$BIZ->error("没有从数据库中返回任何匹配。","");
		
		$pages = $BIZ->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => PAGE_NUM,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=Lst"."&code={$INCOME['code']}&FieldID={$INCOME['FieldID']}".$page_query
											   )
											);
		//--------------------------------------

		$query = " SELECT bp.pic_name,p.shop_id,p.shop_name,p.shop_create_time,p.shop_catalog, p.shop_location,se.seller_logaccount FROM biz_shop AS p , biz_seller AS se ,biz_pic AS bp WHERE  p.seller_id = se.seller_id AND p.shop_prime_picture_id= bp.pic_id AND p.shop_check_status ='checked' AND p.shop_ban_status='off' ".$condition.$order." LIMIT $start,".PAGE_NUM;

		$DB -> db_query( $query );

		while( $sl = $DB -> db_fetch_row() )
		{
			$tm = date("Y-m-d H:i",$sl['shop_create_time']);
			$thumb = $BIZ -> get_thumb($sl['pic_name']);
			$slist[] = array( $sl['shop_id'],$sl['shop_name'],$tm,$sl['shop_location'],$sl['seller_logaccount'],$sl['shop_catalog'],$thumb );
		}
		$BIZ -> nav_link[] = array("店铺列表","");
		$BIZ -> assign( "pages" , $pages);
		$BIZ -> assign( "slist",$slist );
		return;

	}

	function list_goods($flag)
	{
		global $BIZ,$DB,$INCOME;


		
		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		
		
		//---------------------------------------------------
		//判断搜索条件
		//---------------------------------------------------
		switch($flag)
		{
			case 'low':
				$condition = " AND goods_price <=1 AND goods_price >0 ";
			break;
		}

		$condition .= " AND goods_end_active_time >".time(); 

		//---------------------------------------------------
		//判断排序条件
		//---------------------------------------------------
         switch($INCOME[FieldID])
		{
			case '1':
				$order = " ORDER BY `goods_store_date` DESC ";
			    break;
			case '2':
				$order = " ORDER BY `goods_store_date` ASC ";
				break;
			case '3':
				$order = " ORDER BY `goods_price` ASC ";
				break;
			case '4':
				$order = " ORDER BY `goods_price` DESC ";
			    break;
			case '5':
				$order = " ORDER BY `goods_visit_count` DESC ";
			    break;
			default:
				$order = " ORDER BY `goods_store_date` DESC ";
				break;
		}
		
        //---------------------------------------------------
		//paging
		//---------------------------------------------------		
		$DB -> db_query( " SELECT count(*) as count FROM biz_goods WHERE goods_mark_sell='0' ".$condition);

				$row_count = $DB -> db_fetch_row();

		if ($row_count['count'] < 1)
				$BIZ->error("没有从数据库中返回任何匹配。","");
		
		$pages = $BIZ->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => PAGE_NUM,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=Lst&code={$INCOME['code']}&FieldID={$INCOME['FieldID']}".$page_query
											   )
											);

        //--------------------------------------------------

        $query = " SELECT bp.pic_name, g.goods_id,g.goods_name,g.goods_store_date,g.goods_price,g.goods_visit_count,s.seller_logaccount,d.repute FROM biz_goods as g LEFT JOIN biz_pic as bp ON bp.pic_id = g.goods_prime_picture_id LEFT JOIN biz_seller as s  ON g.seller_id = s.seller_id LEFT JOIN biz_seller_data d ON g.seller_id=d.seller_id WHERE goods_mark_sell='0' ".$condition.$order."  LIMIT $start,".PAGE_NUM;
		
		$DB -> db_query( $query );
		$repute_array = array();
		while( $gl = $DB -> db_fetch_row() )
		{
			$tm =  date("Y-m-d H:i",$gl['goods_store_date']);
			$thumb = $BIZ -> get_thumb($gl['pic_name']);
			$glist[] = array($gl['goods_id'],$gl['goods_name'],$tm,$gl['goods_price'],$gl['goods_visit_count'],$gl['seller_logaccount'],$thumb,$gl['repute'],repute_compute($gl['repute']));
		}
		
		$BIZ -> nav_link[] = array("商品列表","");
		$BIZ -> assign( "pages" , $pages);
		$BIZ -> assign( "glist", $glist );
		return;
	}



	function do_search()
	{
		global $BIZ,$DB,$INCOME;

		switch($INCOME[FieldID])
		{
			case '1':
				$order = " ORDER BY `goods_store_date` DESC ";
			    break;
			case '2':
				$order = " ORDER BY `goods_store_date` ASC ";
				break;
			case '3':
				$order = " ORDER BY `goods_price` ASC ";
				break;
			case '4':
				$order = " ORDER BY `goods_price` DESC ";
			    break;
			case '5':
				$order = " ORDER BY `goods_visit_count` DESC ";
			    break;
			default:
				$order = " ORDER BY `goods_store_date` DESC ";
				break;
		}
		//---------------------------------------------------
		//paging
		//---------------------------------------------------
		
		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		$cid =intval($INCOME['CID']);
		$DB -> db_query("SELECT goods_cat_name FROM biz_goods_cat WHERE goods_cat_id =".$cid);
		$cn = $DB -> db_fetch_row();
		
		if($cid) $condition_str = "AND goods_cat_id ='".$cid."' ";
		//-------------------------------------------------
		//filling condition
		//---------------------------------------------------
				
		$DB -> db_query( " SELECT count(*) as count FROM biz_goods WHERE goods_mark_sell='0' ".$condition_str);

				$row_count = $DB -> db_fetch_row();

		if ($row_count['count'] < 1)
				$BIZ->error("没有从数据库中返回任何匹配。","");
		
		$pages = $BIZ->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => PAGE_NUM,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=Lst"."&code={$INCOME['code']}"."&CID={$INCOME['CID']}"."&FieldID={$INCOME['FieldID']}"
											   )
											);

        //--------------------------------------------------

        $query = " SELECT bs.seller_logaccount,pic_name, bg.* FROM biz_goods AS bg , biz_pic ,biz_seller AS bs WHERE pic_id = bg.goods_prime_picture_id  AND bs.seller_id=bg.seller_id AND goods_mark_sell='0' ".$condition_str.$order." LIMIT $start,".PAGE_NUM;
		
		$DB -> db_query( $query );

		while( $gl = $DB -> db_fetch_row() )
		{
			$tm =  date("Y-m-d H:i",$gl['goods_store_date']);
			$thumb = $BIZ -> get_thumb($gl['pic_name']);
			// array($gl['goods_id'],$gl['goods_name'],$tm,$thumb);

			$glist[] = array($gl['goods_id'],$gl['goods_name'],$tm,$gl['goods_price'],$gl['goods_visit_count'],$gl['seller_logaccount'],$thumb);
		}
		
		$BIZ -> assign("cid",$cid);
		$BIZ -> assign( "pages" , $pages);
		$BIZ -> assign( "glist", $glist );
		$BIZ -> assign( "list_nav",$cn[goods_cat_name]);
		return;
	}

	function list_user($id)
	{
		global $BIZ,$DB;

		$rs = $DB -> db_query("SELECT shop_name, shop_catalog,seller_logaccount,shop_visit_count,bs.seller_id,bsp.shop_id,bsp.shop_create_time FROM biz_seller as bs , `biz_seller_data` as bsd ,biz_shop as bsp WHERE bs.seller_id = bsd.seller_id AND bsp.seller_id =bs.seller_id AND bsd.property_id = $id AND bsd.end_time >=".time()." ORDER BY end_time DESC" );

		while( $user_array= $DB -> db_fetch_row() )
		{
             $user_array['shop_create_time'] = date("Y-m-d",$user_array['shop_create_time']);
			 $user_info[] = array($user_array['seller_id'],$user_array['seller_logaccount'],$user_array['shop_name'],$user_array['shop_catalog'],$user_array['shop_visit_count'],$user_array['shop_id'],$user_array['shop_create_time']);
		}

		$BIZ -> assign('user_info',$user_info);
	}
}
?>